package com.ccb.sc.common.modules.order.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.ccb.sc.common.entity.BaseFillEntity;
import com.ccb.sc.common.modules.order.enums.CommentStatus;
import com.ccb.sc.common.modules.order.enums.DeliverStatus;
import com.ccb.sc.common.modules.order.enums.OrderStatus;
import com.ccb.sc.common.modules.payment.enums.OrderType;
import com.ccb.sc.common.modules.payment.enums.PaymentStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.beans.Transient;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 订单表
 * </p>
 *
 * @author ken
 * @since 2020-09-25
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("tb_order")
public class Order extends BaseFillEntity {

    private static final long serialVersionUID=1L;

    @TableId(value = "id", type = IdType.INPUT)
    private String id;

    /**
     * 所属用户
     */
    @TableField("member_id")
    private Long memberId;

    /**
     * 用户名称
     */
    @TableField("username")
    private String username;

    /**
     * 订单金额
     */
    @TableField("amount")
    private BigDecimal amount;

    /**
     * 消耗积分
     */
    @TableField("use_points")
    private BigDecimal usePoints;

    /**
     * 积分抵扣金额
     */
    @TableField("points_deduct_amount")
    private BigDecimal pointsDeductAmount;

    /**
     * 钱包余额抵扣金额
     */
    @TableField("wallet_deduct_amount")
    private BigDecimal walletDeductAmount;

    /**
     * 实付金额
     */
    @TableField("real_amount")
    private BigDecimal realAmount;

    /**
     * 到期时间
     */
    @TableField("expiry_time")
    private LocalDateTime expiryTime;

    /**
     * 付款时间
     */
    @TableField("payment_time")
    private LocalDateTime paymentTime;

    /**
     * 支付方式
     */
    @TableField("payment_method")
    private String paymentMethod;

    /**
     * 支付状态
     */
    @TableField("payment_status")
    private PaymentStatus paymentStatus;

    /**
     * 支付凭证
     */
    @TableField("trade_no")
    private String tradeNo;

    /**
     * 获得积分
     */
    @TableField("reward_points")
    private BigDecimal rewardPoints;

    /**
     * 运费
     */
    @TableField("freight")
    private BigDecimal freight;

    /**
     * 用户备注
     */
    @TableField("user_memo")
    private String userMemo;

    /**
     * 区域id
     */
    @TableField("region_id")
    private Long regionId;

    /**
     * 收货地址
     */
    @TableField("address")
    private String address;

    /**
     * 收货人
     */
    @TableField("consignee")
    private String consignee;

    /**
     * 联系电话
     */
    @TableField("phone")
    private String phone;

    /**
     * 收货信息id
     */
    @TableField("receiver_id")
    private Long receiverId;

    /**
     * 发货时间
     */
    @TableField("deliver_time")
    private LocalDateTime deliverTime;

    /**
     * 物流公司
     */
    @TableField("logistics_company")
    private String logisticsCompany;

    /**
     * 物流单号
     */
    @TableField("logistics_no")
    private String logisticsNo;

    /**
     * 收货时间
     */
    @TableField("receiver_time")
    private LocalDateTime receiverTime;

    /**
     * 评价时间
     */
    @TableField("comment_time")
    private LocalDateTime commentTime;

    /**
     * 评价时间
     */
    @TableField("finish_time")
    private LocalDateTime finishTime;

    /**
     * 物流状态
     */
    @TableField("deliver_status")
    private DeliverStatus deliverStatus;

    /**
     * 评价状态
     */
    @TableField("comment_status")
    private CommentStatus commentStatus;

    /**
     * 订单类型 1积分订单 2商城订单
     */
    @TableField("order_type")
    private OrderType orderType;

    /**
     * 邀请人id
     */
    @TableField("invite_id")
    private Long inviteId;

    /**
     * 订单状态
     */
    @TableField("status")
    private OrderStatus status;

    /**
     * 是否删除
     */
    @TableField("deleted")
    @TableLogic
    private Boolean deleted;


    @Transient
    public boolean isExpired() {
        return getExpiryTime() != null && LocalDateTime.now().isAfter(getExpiryTime());
    }
}
